package com.lagou.web;

import com.lagou.entity.Result;
import com.lagou.entity.Student;
import com.lagou.service.StudentInfoService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.util.List;

public class StudentInfoServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {

        if (req.getMethod().equalsIgnoreCase("post"))
            req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        // 获取请求的URI地址信息
        String url = req.getRequestURI();
        // 截取其中的方法名
        String methodName = url.substring(url.lastIndexOf("/") + 1, url.lastIndexOf(".do"));
        Method method = null;
        try {
            // 使用反射机制获取在本类中声明了的方法
            method = getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            System.out.println("反射的方法名是：" + method.getName());
            // 执行方法
            method.invoke(this, req, resp);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("调用方法出错！");
        }
    }

    private void add(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String stuId = request.getParameter("stuId");
        String name = request.getParameter("name");
        String sex = request.getParameter("sex");
        String birthday = request.getParameter("birthday");
        boolean flag = StudentInfoService.update(new Student(-1, stuId, name, sex, birthday)) > 0;
        if (flag)
            response.sendRedirect("/web_stu_info/home.html");
//            response.getWriter().print(Result.toJsonString(1, "home.html", "添加成功"));
        else
            response.getWriter().print(Result.toJsonString(-99, "", "添加失败"));
    }

    private void query(HttpServletRequest request, HttpServletResponse response) {
        try {
            String keyword = request.getParameter("keyWord");
            Student student = StudentInfoService.query(keyword);
            String json = student.toJsonString();
            response.getWriter().println(json);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void queryAll(HttpServletRequest request, HttpServletResponse response) {
        try {
            List<Student> studentList = StudentInfoService.queryAll();
            String json = Student.toJsonString(studentList);
            response.getWriter().println(json);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void load(HttpServletRequest request, HttpServletResponse response) {
    }


}
